package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MyApiController {

    private final RestTemplate restTemplate;

    public MyApiController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping()
    public Object handleRequest(@RequestParam String text) throws IOException {
        System.out.println(text);
        // 构造请求体为 FormData
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("text", text);
        requestBody.add("prompt", "");
        requestBody.add("voice", "1111.csv");
        requestBody.add("speed", 5);
        requestBody.add("temperature", 0.3);
        requestBody.add("top_p", 0.7);
        requestBody.add("top_k", 20);
        requestBody.add("refine_max_new_token", 384);
        requestBody.add("infer_max_new_token", 2048);
        requestBody.add("text_seed", 42);
        requestBody.add("skip_refine", 0);
        requestBody.add("is_stream", 0);
        requestBody.add("custom_voice", 0);

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        // 构造 HttpEntity
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(requestBody, headers);

        // 请求地址
        String requestUrl = "http://127.0.0.1:9966/tts";

        // 发送 POST 请求
        ResponseEntity<String> response = restTemplate.exchange(
                requestUrl,
                HttpMethod.POST,
                entity,
                String.class);

        System.out.println(response.getBody());
        // 解析 JSON 字符串
        JSONObject jsonObject = JSON.parseObject(String.valueOf(response.getBody()));
//        return null;

        Path path = Paths.get((String) jsonObject.get("filename"));
        byte[] audioBytes = Files.readAllBytes(path);

        // 将音频字节数组封装为 ByteArrayResource
        ByteArrayResource resource = new ByteArrayResource(audioBytes);

        // 设置响应头
        HttpHeaders headers2 = new HttpHeaders();
        headers2.setContentType(MediaType.parseMediaType("audio/mpeg")); // 设置为音频格式
        headers2.setContentLength(resource.contentLength());
        headers2.setContentDisposition(ContentDisposition.builder("inline")
                .filename("sample.mp3")
                .build());
        // 返回 ResponseEntity
        return ResponseEntity.ok()
                .headers(headers2)
                .body(resource);
    }
}
